/**
 * Created by Administrator on 2016/7/28.
 */
"use strict";
swan_pc
    .controller("ClipBookDetailController",function($state,$scope,Clip,TimeExchange,$rootScope){
        $scope.ClipKey={
            "name":"日程内容",
            "startTime":"开始时间",
            "endTime":"结束时间",
            "address":"日程地点",
            "level":"层次",
            "status":"状态",
            "actionRemark": "行动备注",
            "advanceInfo": "高级信息",
            "category": "公事/私事",
            "deletedAt": "删除时间",
            "isDelayed": "是否拖延",
            "isFixed": "是否固定时间",
            "isImportant": "是否重要",
            "isPrincipal": "是否属于组织",
            "isSecret": "是否私密",
            "linkedActivityId": "link活动Id",
            "linkedActivityType": "link活动类型",
            "organizationId": "组织ID",
            "ownerId": "UserID",
            "previewTime": "预计时间",
            "relatedCompany": "公司",
            "relatedContact": "相关联系人",
            "relatedOpp": "机会",
            "remark": "备注",
            "remind": "提醒时间",
            "type": "类型",
            "who": "行动对象"
        }
        $scope.ClipBookedDetail={
            "name":null,
            "startTime":null,
            "endTime":null,
            "address":null,
            "level":null,
            "status":null,
            "actionRemark": null,
            "advanceInfo": null,
            "category": null,
            "deletedAt": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact": null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "type": null,
            "who": null
        }
        $scope.$watch(function(){
            return $scope.$parent.clipBookedId;
        },function(){
            Clip.get({clipid:$scope.$parent.clipBookedId},function(result){
                $scope.ClipBookedDetail=result;
            })
        })
        $scope.status_show=function(option){
            if(option=="DRAFT"||option=="草稿") return "草稿";
            if(option=="CONFIRM"||option=="确认") return "确认";
            if(option=="CANCELED"||option=="取消") return "取消";
            if(option=="CLOSED"||option=="关闭") return "关闭";
            if(option=="PENDING"||option=="待定") return "待定";
        }
        $scope.judge_show=function(option){
            if(option=="1") return "是";
            if(option=="0") return "否";
        }
        $scope.TimeShow=function(time){
            if(time==null){
                return "";
            }
            var timer=time.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            //console.log("clipdetail"+time + " " + timer);
            return timer;
        };
        $scope.init_show=function(){
            $scope.InfoShow=true;
        }
        $scope.init_show();
        $scope.display=function(option){
            if(option==0){
                $scope.InfoShow=false;
            }
            if(option==1){
                $scope.InfoShow=true;
            }
        }
        $scope.confirmClip=function(){
            $scope.ClipBookedDetail.status="CONFIRM";
            Clip.update({}, $scope.ClipBookedDetail,
                function (result) {
                    $scope.$parent.loadClipList();
                    swal("您已接受好友日程安排", $scope.ClipBookedDetail.name, "success");
                    $state.go("calendarshow");
                },
                function (result) {
                    if(result.status=="409"){
                        swal("添加失败", "您当前接受的日程与原有日程时间冲突，请修改后提交", "error");
                    }
                    swal("接受好友日程安排失败",$scope.ClipBookedDetail.name, "error");
                })
            }
        $scope.refuseClip=function(){
            $scope.ClipBookedDetail.status="CANCEL";
            Clip.update({}, $scope.ClipBookedDetail,
                function (result) {
                    $scope.$parent.loadClipList();
                    swal("您已拒绝好友日程安排", $scope.ClipBookedDetail.name, "success");
                    $state.go("calendarshow");
                },
                function (result) {
                    swal("拒绝好友日程安排失败",$scope.ClipBookedDetail.name, "error");
                })
        }
        $scope.DeleteThisClip=function(){
            swal({
                title: "确定要删除该日程吗",
                text: "删除后将无法恢复！！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消删除",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    Clip.delete({clipid:$scope.$parent.clipBookedId},
                        function(result){
                            $scope.$parent.ClipListHasChanged=0?1:0;
                            $scope.$parent.loadClipList();
                            swal("删除成功",$scope.ClipBookedDetail.name,"success");
                            $state.go("calendarshow");
                        },
                        function(result){
                            swal("删除失败",$scope.ClipBookedDetail.name,"error");
                        })
                }else
                {     swal("删除取消",$scope.ClipBookedDetail.name,"error");
                }
            });
        };
        $scope.BackToCal=function(){
            $state.go("calendarshow");
        }
    })